"""
@author：yiwang
@file_name: __init__.py
@create date: 2023-12-08
@file_description：
"""

from apps.NGS_API.connector.db_common import *
import re

class id_generator(object):
    
    def __init__(self,name):
        self.count = 0
        self.name = name
        self.init = {
            "project_id":"2023033099000",
            "sample_id":"992023033099000",
            "stock_id":"2023033099000",
            "analysis_id":"33099000",
            "act_id":"202303390000"	
        }
        self.prefix = {
            "project":"Z",
            "sample":"DT",
            "extraction":"EXT",
            "bioana":"ANA",
            "stock":"STC",
            "act":"ACT"
        } 
    
    def generate_prefix(self,tag):
        return self.prefix[tag]
    
    def check_previous_data(self):
        pass
    
    def _Max_id(self,numbers):
        max = 0
        for num in numbers:
            if num > max:
                max = num
            else:
                pass
        return max
    
    def generate_id(self,col:str):
        query_info_sql = '''
            SELECT
                id,
                {col}
            FROM {table_name}
        '''.format(
            col = col,
            table_name = self.name
        )
        ### SQL: query result infos ###
        result_infos = query_info(query_info_sql)
        if not result_infos:
            return {"id":0,col:self.init[col]}
        else:
            ids = [ tmp[0] for tmp in result_infos ]
            max_id = self._Max_id(ids)
            tmp_ids = [ int(re.sub("[A-Za-z]","",result[1])) for result in result_infos ]
            max_col_id = self._Max_id(tmp_ids)
            return {"id":max_id,col:max_col_id}